﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;

namespace Sale_Operation
{
    public class RecordListOp : Component
    {
        public override bool Save(object obj)
        {
            bool blRes = false;
            RecordList SM = obj as RecordList; 
        

            try
            {
                StringBuilder sbSql = new StringBuilder();
                if (SM.RList_Id == 0)
                {
                    sbSql.Append("INSERT INTO RecordList (RList_Code,RList_Title,RList_Des,RList_Money,RList_BData_Id,RList_StartTime,RList_EndTime,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append("RList_Code='" + ValueHandler.GetStringValue(SM.RList_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("RList_Title='" + ValueHandler.GetStringValue(SM.RList_Title, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("RList_Des='" + ValueHandler.GetStringValue(SM.RList_Des, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("RList_Money='" + ValueHandler.GetStringValue(SM.RList_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("RList_BData_Id='" + ValueHandler.GetStringValue(SM.RList_BData_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("RList_StartTime='" + ValueHandler.GetStringValue(SM.RList_StartTime, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("RList_EndTime='" + ValueHandler.GetStringValue(SM.RList_EndTime, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("State=1,");
                    sbSql.Append("CreateTime=getdate(),");
                    sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SM.RList_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "新增费用支出信息[" + ValueHandler.GetStringValue(SM.RList_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.费用_支出);

                }
                else
                {
                    sbSql.Append("UPDATE RecordList SET ");
                    sbSql.Append("RList_Code='" + ValueHandler.GetStringValue(SM.RList_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("RList_Title='" + ValueHandler.GetStringValue(SM.RList_Title, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("RList_Des='" + ValueHandler.GetStringValue(SM.RList_Des, ValueHandler.CharHandlerType.IsRepChar) + "',");                   
                    sbSql.Append("RList_Money='" + ValueHandler.GetStringValue(SM.RList_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("RList_BData_Id='" + ValueHandler.GetStringValue(SM.RList_BData_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("RList_StartTime='" + ValueHandler.GetStringValue(SM.RList_StartTime, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("RList_EndTime='" + ValueHandler.GetStringValue(SM.RList_EndTime, ValueHandler.CharHandlerType.IsRepChar) + "'  ");
                    sbSql.Append("WHERE RList_Id=" + SM.RList_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "修改费用支出户信息[" + ValueHandler.GetStringValue(SM.RList_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.费用_支出);
                }
                blRes = true;

            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增/修改费用支出信息出错！[" + ex.ToString() + "]", EMenuList.费用_支出);
            }
            db.Close();
            return blRes;
        }

        public override bool HasExists(object obj)
        {
            RecordList SM = obj as RecordList; 
            bool blRes = true;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM RecordList WHERE RList_Code='" + SM.RList_Code + "' AND RList_Id<>" + SM.RList_Id + " AND State=1 ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;

            }
            catch (Exception ex)
            {
            }
            db.Close();
            return blRes;
        }

        public override bool Delete(object obj)
        {
            bool blRes = false;
            try
            {
                db = new DataBase();
                RecordList SM = obj as RecordList;
    
                StringBuilder sbSql = new StringBuilder();
                db.Open();
                sbSql.Append(" UPDATE RecordList SET State = 0 WHERE RList_Id=" + SM.RList_Id);
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString()); 
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "支出单[" + SM.RList_Code + "]删除完成！", EMenuList.费用_支出);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "支出单删除出错！[" + ex.ToString() + "]", EMenuList.费用_支出);
            }
            db.Close();
            return blRes;
        }

        public override DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT RList_Id,RList_Code,RList_Title,RList_Des,RList_Money,RList_BData_Id,B.BData_Name ,RList_StartTime,RList_EndTime,A.State,A.CreateTime ,A.CreateMan FROM RecordList A ");
                sbSql.Append(" LEFT JOIN BaseData B ON (A.RList_BData_Id=B.BData_Id)");
                sbSql.Append(" WHERE A.State= 1 ");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "费用支出加载失败！[" + ex.ToString() + "]", EMenuList.费用_支出);
            }
            db.Close();
            return db.Data.Tables[0];
        }

        public override object GetModel(object obj)
        {
            RecordList SM = obj as RecordList;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT RList_Id,RList_Code,RList_Title,RList_Des,RList_Money,RList_BData_Id,B.BData_Name ,RList_StartTime,RList_EndTime,A.State,A.CreateTime ,A.CreateMan FROM RecordList A ");
                sbSql.Append(" LEFT JOIN BaseData B ON (A.RList_BData_Id=B.BData_Id)");
                sbSql.Append(" WHERE A.State= 1 AND RList_Id=" + SM.RList_Id);
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
                if (db.Data.Tables.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.RList_BData_Id = ValueHandler.GetIntNumberValue(dr["RList_BData_Id"]);
                    SM.RList_Code = ValueHandler.GetStringValue(dr["RList_Code"]);
                    SM.RList_Des = ValueHandler.GetStringValue(dr["RList_Des"]);
                    SM.RList_EndTime = DateTime.Parse(dr["RList_EndTime"].ToString());
                    SM.RList_Money = ValueHandler.GetDecNumberValue(dr["RList_Money"]);
                    SM.RList_StartTime = DateTime.Parse(dr["RList_StartTime"].ToString());
                    SM.RList_Title = ValueHandler.GetStringValue(dr["RList_Title"]);
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);

                }
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "费用支出加载失败！[" + ex.ToString() + "]", EMenuList.费用_支出);
            }
            db.Close();
            return SM;
        }
    }
}
